<?php
/**
 * 通用页面
 *
 *
 **你正在使用的程序由BESONIT提供*/

//defined('BYshopJL') or exit('Access Invalid!');

class orderControl extends SystemControl{
	public function __construct(){
		parent::__construct();
	}

	/**
	 * 佣金发放
	 * Author: walkskyer
	 * Email:zwj_work@qq.com
	 */
	public function refresh_brokerageOp(){
        global $config;
		/* @var cron_orderModel $order_model */
		echo "123\n";
		$order_model = Model('cron_order');
		echo "456\n";
		/* @var cron_bs_brokerageModel $bs_brokerage_model */
		$bs_brokerage_model = Model('cron_bs_brokerage');
		/* @var cron_bs_profit_logModel $bs_profit_log_model */
		$bs_profit_log_model = Model('cron_bs_profit_log');

		$target_time = time();//-86400*1
		$page_num=1;
		$limit=2;
		$sql_tpl="SELECT * FROM %s WHERE brokerage_time=0 AND refund_state=0 AND finnshed_time>0 AND  finnshed_time <= %s limit %s,%s;";//
		$order_table_name=$order_model->getTableName();
		header('Content-type: text/html; charset=utf-8');
		echo "789\n";
		while($order_list=$order_model->query(sprintf($sql_tpl,$order_table_name,$target_time,($page_num-1)*$limit,$limit))){//finnshed_time > 0 AND

			echo "abc".time()."\n";
			var_dump($order_list);

			foreach($order_list as $order){
				if(!$bs_brokerage_model->where('bk_member_id='.$order['brokerage_account'])->select()){
					$bs_brokerage_model->insert(array('bk_member_id'=>$order['brokerage_account']));
				}
				$bs_brokerage_model->query(sprintf('UPDATE %s SET bk_amount = bk_amount + %s , bk_total = bk_total + %s WHERE bk_member_id=%s;',
					$bs_brokerage_model->getTableName(),$order['brokerage'],$order['brokerage'],$order['brokerage_account']));
				$bs_profit_log_model->insert(array(
					'order_id'=>$order['order_id'],
					'member_id'=>$order['brokerage_account'],
					'type'=>1,
					'store_id'=>$order['store_id'],
					'amount'=>$order['brokerage'],
					'note'=>'佣金发放',
					'modified'=>time(),
					'dateline'=>time(),
				));
				$order_model->update(array('brokerage_time'=>time()),array('where'=>'order_id='.$order['order_id']));

                if($order['brokerage_account']>0){
                    //发送微信消息，发给分享者
                    require_once(BASE_ROOT_PATH.'/weixin/framework/libraries/wxSdk.class.php');
                    $wxSdk = new wxSdk();
                    $wxSdk->Touser(array('member_id'=>$order['brokerage_account']));
                    $wxSdk->TemplateUrl(1,$config['site_url'].'/wx/tmpl/member/member.html');
                    $wxSdk->setTemplateTopColor();
                    $data = array(
                        'order_sn' =>$order['order_sn'],
                        'order_amount' =>$order['order_amount'],
                        'brokerage' =>$order['brokerage']
                    );
                    $wxSdk->setTemplateData(8,$data);
                    $wxSdk->sendTemplateMessage();
                }


			}
			$page_num++;
		}

		$page_num=1;
		$sql_tpl="SELECT * FROM %s WHERE brokerage_time=0 AND refund_state=0 AND  finnshed_time = 0 limit %s,%s;";//finnshed_time>0 AND
		$order_table_name=$order_model->getTableName();
		header('Content-type: text/html; charset=utf-8');
		echo "789\n";
		$bs_brokerage_model->query(sprintf('UPDATE %s SET bk_anticipated = 0 where 1',$bs_brokerage_model->getTableName()));
		while($order_list=$order_model->query(sprintf($sql_tpl,$order_table_name,($page_num-1)*$limit,$limit))){//finnshed_time > 0 AND

			echo "abc".time()."\n";
			var_dump($order_list);

			foreach($order_list as $order){
				if(!$bs_brokerage_model->where('bk_member_id='.$order['brokerage_account'])->select()){
					$bs_brokerage_model->insert(array('bk_member_id'=>$order['brokerage_account']));
				}
				$bs_brokerage_model->query(sprintf('UPDATE %s SET bk_anticipated = bk_anticipated + %s WHERE bk_member_id=%s;',
					$bs_brokerage_model->getTableName(),$order['brokerage'],$order['brokerage_account']));
			}
			$page_num++;
		}
	}

}
